/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.news.dao.impl;
import com.ktjiaoyu.news.dao.BaseDao;
import com.ktjiaoyu.news.dao.NewsAdminDao;
import com.ktjiaoyu.news.entity.NewsAdmin;
import com.ktjiaoyu.news.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * 管理员信息表，用于登录后台 
 * @author 科泰集团教研团队
 * @createTime 2024-4-9 17:36:33
 * @lastModify 2024-4-9 17:36:33
 * @version 1.0.0
 */
public class NewsAdminDaoImpl extends BaseDao implements NewsAdminDao {

    @Override
    public User getByUsername(String username) {
        User user = null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            // 获取连接
            conn = getConnection();
            String sql = "select * from t_user where loginName = ? ";
            // 获取执行sql对象，设置参数
            stmt = conn.prepareStatement(sql);
            stmt.setObject(1, username);
            // 执行获得结果
            rs = stmt.executeQuery();
            while(rs.next()){
                user = new User();
                user.setRoleId(rs.getInt("roleId"));
                user.setLoginName(rs.getString("loginName"));
                user.setUserName(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setSex(rs.getInt("sex"));
                user.setIdentityCode(rs.getString("identityCode"));
                user.setEmail(rs.getString("email"));
                user.setMobile(rs.getString("mobile"));
                user.setEnabled(rs.getInt("enabled"));
                user.setCreateTime(rs.getString("create_time"));
                user.setUpdateTime(rs.getString("update_time"));
                user.setAvatar(rs.getString("avatar"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn, stmt, rs);
        }
        return user;
    }

    @Override
    public List<NewsAdmin> getPage(NewsAdmin entity, Integer pageNo, Integer pageSize) {
        List<NewsAdmin> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            List<Object> params = new ArrayList<>();
            String sql = "select * from news_admin where 1=1 ";
            if(entity != null){
                if(entity.getId() != null){
                    sql += " and id = ?";
                    params.add(entity.getId());
                }
                if(entity.getUsername() != null){
                    sql += " and username = ?";
                    params.add(entity.getUsername());
                }
                if(entity.getPassword() != null){
                    sql += " and password = ?";
                    params.add(entity.getPassword());
                }
                if(entity.getNickname() != null){
                    sql += " and nickname = ?";
                    params.add(entity.getNickname());
                }
                if(entity.getAvatar() != null){
                    sql += " and avatar = ?";
                    params.add(entity.getAvatar());
                }
            }
            // sql += " order by xxx , xxx desc"

            sql += " limit ?, ?";
            params.add((pageNo - 1) * pageSize);
            params.add(pageSize);
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.size(); i++) {
                stmt.setObject(i + 1, params.get(i));
            }
            rs = stmt.executeQuery();
            while(rs.next()){
                NewsAdmin newsAdmin = new NewsAdmin();
                newsAdmin.setId(rs.getLong("id"));
                newsAdmin.setUsername(rs.getString("username"));
                newsAdmin.setPassword(rs.getString("password"));
                newsAdmin.setNickname(rs.getString("nickname"));
                newsAdmin.setAvatar(rs.getString("avatar"));
                list.add(newsAdmin);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn, stmt, rs);
        }
        return list;
    }

    @Override
    public int count(NewsAdmin entity) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            List<Object> params = new ArrayList<>();
            String sql = "select count(1) from news_admin where 1=1 ";
            if(entity != null){
                if(entity.getId() != null){
                    sql += " and id = ?";
                    params.add(entity.getId());
                }
                if(entity.getUsername() != null){
                    sql += " and username = ?";
                    params.add(entity.getUsername());
                }
                if(entity.getPassword() != null){
                    sql += " and password = ?";
                    params.add(entity.getPassword());
                }
                if(entity.getNickname() != null){
                    sql += " and nickname = ?";
                    params.add(entity.getNickname());
                }
                if(entity.getAvatar() != null){
                    sql += " and avatar = ?";
                    params.add(entity.getAvatar());
                }
            }
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.size(); i++) {
                stmt.setObject(i + 1, params.get(i));
            }
            rs = stmt.executeQuery();
            while(rs.next()){
                return rs.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn, stmt, rs);
        }
        return 0;
    }

    @Override
    public List<NewsAdmin> getAll() {
        List<NewsAdmin> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            List<Object> params = new ArrayList<>();
            String sql = "select * from news_admin";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while(rs.next()){
                NewsAdmin newsAdmin = new NewsAdmin();
                newsAdmin.setId(rs.getLong("id"));
                newsAdmin.setUsername(rs.getString("username"));
                newsAdmin.setPassword(rs.getString("password"));
                newsAdmin.setNickname(rs.getString("nickname"));
                newsAdmin.setAvatar(rs.getString("avatar"));
                list.add(newsAdmin);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn, stmt, rs);
        }
        return list;
    }

    @Override
    public NewsAdmin getById(Long id) {
        NewsAdmin newsAdmin = null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            // 获取连接
            conn = getConnection();
            List<Object> params = new ArrayList<>();
            String sql = "select * from news_admin where id=?";
            // 获取执行sql对象，设置参数
            stmt = conn.prepareStatement(sql);
            stmt.setObject(1, id);
            // 执行获得结果
            rs = stmt.executeQuery();
            while(rs.next()){
                newsAdmin = new NewsAdmin();
                newsAdmin.setId(rs.getLong("id"));
                newsAdmin.setUsername(rs.getString("username"));
                newsAdmin.setPassword(rs.getString("password"));
                newsAdmin.setNickname(rs.getString("nickname"));
                newsAdmin.setAvatar(rs.getString("avatar"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn, stmt, rs);
        }
        return newsAdmin;
    }

    @Override
    public int insert(NewsAdmin entity) {
        return executeUpdate("INSERT INTO news_admin(`username`,`password`,`nickname`,`avatar`) VALUES (?,?,?,?);"
        ,entity.getUsername(),entity.getPassword(),entity.getNickname(),entity.getAvatar());
    }

    @Override
    public int[] insertBatch(NewsAdmin... entities) {
        int[] rows = null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            conn.setAutoCommit(false);
            String sql = "INSERT INTO newsAdmin(`username`,`password`,`nickname`,`avatar`) VALUES (?,?,?,?);";
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < entities.length; i++) {
                NewsAdmin entity = entities[i];
                if(entity == null){
                    continue;
                }
                int j = 1;
                stmt.setObject(j++, entity.getUsername());
                stmt.setObject(j++, entity.getPassword());
                stmt.setObject(j++, entity.getNickname());
                stmt.setObject(j++, entity.getAvatar());
                stmt.addBatch();
            }
            rows = stmt.executeBatch();
            stmt.clearBatch();
            conn.commit();
        }catch (Exception e){
            e.printStackTrace();
            if(conn != null){
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }finally {
            closeAll(conn, stmt, rs);
        }
        return rows;
    }

    @Override
    public int update(NewsAdmin entity) {
        return executeUpdate("UPDATE `news_admin` " +
                        " SET  `username` = ?,`password` = ?,`nickname` = ?,`avatar` = ? WHERE id = ? "
,entity.getUsername(),entity.getPassword(),entity.getNickname(),entity.getAvatar(),entity.getId());
    }

    @Override
    public int deleteByEntity(NewsAdmin entity)  {
        if(entity == null){
            return 0;
        }
        List<Object> params = new ArrayList<>();
        String sql = "delete from news_admin where 1=1 ";
                if(entity.getId() != null){
                    sql += " and id = ?";
                    params.add(entity.getId());
                }
                if(entity.getUsername() != null){
                    sql += " and username = ?";
                    params.add(entity.getUsername());
                }
                if(entity.getPassword() != null){
                    sql += " and password = ?";
                    params.add(entity.getPassword());
                }
                if(entity.getNickname() != null){
                    sql += " and nickname = ?";
                    params.add(entity.getNickname());
                }
                if(entity.getAvatar() != null){
                    sql += " and avatar = ?";
                    params.add(entity.getAvatar());
                }
        if(params.size() == 0){
            return 0;
        }
        return executeUpdate(sql, params.toArray());
    }

    @Override
    public int deleteById(Long id) {
        return executeUpdate("delete from news_admin where id=?", id);
    }

    @Override
    public int[] deleteBatch(Long... ids) {
        int[] rows = null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            conn.setAutoCommit(false);
            String sql = "delete from news_admin where id=?";
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < ids.length; i++) {
                Long id = ids[i];
                if(id == null){
                    continue;
                }
                int j = 1;
                stmt.setObject(j++, id);
                stmt.addBatch();
            }
            rows = stmt.executeBatch();
            stmt.clearBatch();
            conn.commit();
        }catch (Exception e){
            e.printStackTrace();
            if(conn != null){
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }finally {
            closeAll(conn, stmt, rs);
        }
        return rows;
    }
}
